package com.tek.ding.faceding.ui.face;

import android.content.Context;
import android.content.Intent;
import android.widget.EditText;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jakewharton.rxbinding4.widget.RxTextView;
import com.lxj.xpopup.XPopup;
import com.tek.ding.faceding.R;
import com.tek.ding.faceding.base.BaseActivity;
import com.tek.ding.faceding.bean.BaseBean;
import com.tek.ding.faceding.bean.Members;
import com.tek.ding.faceding.presenter.face.FaceContract;
import com.tek.ding.faceding.presenter.face.FaceModel;
import com.tek.ding.faceding.presenter.face.FacePresenter;
import com.tek.ding.faceding.ui.camera.CameraOpenGLActivity;
import com.tek.ding.faceding.utils.BaseTools;
import com.tek.ding.faceding.utils.CheckNetwork;
import com.tek.ding.faceding.utils.Constants;
import com.tek.ding.faceding.utils.MyToastUtils;
import com.tek.ding.faceding.utils.SPUtils;
import com.tek.ding.faceding.utils.popup.PopupTip;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class InputFaceActivity extends BaseActivity<FacePresenter> implements FaceContract.FaceView, MemberAdapter.OnItemClickListener {

    private String name = "";
    private MemberAdapter adapter;
    private List<Members> memberList = new ArrayList<>();

    private Members members;

    private PopupTip tipPopupView;
    private static Disposable mDisposable;

    private String storeUid;

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, InputFaceActivity.class));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_input_face;
    }

    @Override
    protected String setTitleName() {
        return "人脸录入";
    }

    @Override
    protected FacePresenter createPresenter() {
        return new FacePresenter(new FaceModel(), this);
    }

    @Override
    protected void init() {

        storeUid = SPUtils.getString(Constants.STORE_UID, "");

        RecyclerView recyclerView = find(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        adapter = new MemberAdapter(this, recyclerView, memberList);
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);

        EditText editText = find(R.id.editText);
        editText.requestFocus();
        RxTextView.textChanges(editText)
                .debounce(1000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s -> {
                    if (!CheckNetwork.isNetworkConnected(this)) {
                        MyToastUtils.showLong(getResources().getText(R.string.error_network));
                    } else {
                        String str = s.toString();
                        name = str;
                        if (StringUtils.isNoneBlank(str)) {
                            BaseTools.hideSoftKeyBoard(this);
                            presenter.getMemberList2(storeUid, str);
                        }
                    }
                });
    }

    @Override
    public void onLoadOk(BaseBean<List<Members>> baseBean) {
        if (baseBean.success()) {
            List<Members> list = baseBean.getData();
            if (CollectionUtils.isNotEmpty(list)) {
                adapter.setData(list);
            } else {
                MyToastUtils.showLong("没有查到会员");
            }
        } else {
            MyToastUtils.showLong(baseBean.getMsg());
        }
    }

    @Override
    public void onLoadError(String throwable) {
        MyToastUtils.showLong(throwable);
    }

    @Override
    public void onDelOk(BaseBean<String> baseBean) {
        if (baseBean.success()) {
            MyToastUtils.showLong("删除成功，请重新录入人脸");
            Observable.timer(1, TimeUnit.SECONDS)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<Long>() {
                        @Override
                        public void onSubscribe(@NonNull Disposable d) {
                            mDisposable = d;
                        }

                        @Override
                        public void onNext(@NonNull Long aLong) {

                        }

                        @Override
                        public void onError(@NonNull Throwable e) {

                        }

                        @Override
                        public void onComplete() {
                            CameraOpenGLActivity.startActivity(InputFaceActivity.this, members, 1);
                        }
                    });
        } else {
            MyToastUtils.showLong(baseBean.getMsg());
        }
    }

    @Override
    public void onDelError(String throwable) {
        MyToastUtils.showLong(throwable);
    }

    @Override
    public void onItemClickListener(Members member) {
        this.members = member;

        if (members.getFaceUpStatus() != null && members.getFaceUpStatus().equals(1)) {
            tipPopupView = new PopupTip(this);
            tipPopupView.setTitleContent("", "人脸已录入，是否删除重新录入？", "");
            tipPopupView.setListener(() -> {
                if (!CheckNetwork.isNetworkConnected(this)) {
                    MyToastUtils.showLong(getResources().getText(R.string.error_network));
                } else {
                    presenter.deleteFaceEntity2(storeUid, members.getUid());
                }
            }, null);
            this.tipPopupView = (PopupTip) new XPopup.Builder(this)
                    .autoOpenSoftInput(false)
                    .dismissOnTouchOutside(true)
                    .asCustom(tipPopupView);
            tipPopupView.show();
        } else {
            CameraOpenGLActivity.startActivity(this, members, 1);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (StringUtils.isNoneBlank(name)) {
            presenter.getMemberList2(storeUid, name);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
        }
    }
}
